package com.ccx.coachsite.ui.share;


import android.content.Context;
import android.view.LayoutInflater;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.customview.LazyViewPager;
import com.ccx.coachsite.ui.customview.ViewPagerTab;
import com.ccx.coachsite.util.SharedPreferencesUtil;


public class OpenShareTabLazyView extends CommonAppView {
	
	public static final String TAG = "OpenShareTabLazyView";

	private Context ctx;
	
	/**1、消息推送  2、短信分享*/
	private int mCurrentType = 1;
	public LazyViewPager vp;
	public ViewPagerTab tab;
	
	private MobilePhoneView mobilePhoneView;
	private MyCustomerView myCustomerView;
	private PhoneBookView phoneBookView;
	private OpenSendListener mOpenSend;
	
	private final int[] names = { R.string.openshare_mobilephone, R.string.openshare_mycustomer, 
			R.string.openshare_phonebook };
	
	public int initTab = 0;  //默认加载的Tab序号
	
	
	/**
	 * 
	 * <p>Title: </p> 
	 * <p>Description: </p> 
	 * @param paramContext
	 * @param openSend
	 * @param currentType 1、消息推送  2、短信分享
	 */
	public OpenShareTabLazyView(Context paramContext, OpenSendListener openSend, int currentType) {	
		super(paramContext);

		mCurrentType = currentType;
		
		mOpenSend = openSend;
		
		ctx = paramContext;
		
		boolean isExport = SharedPreferencesUtil.getBoolean(ctx, SharedKey.Shared_Is_Expert);
		
		addView(R.layout.open_share_viewpager);
		
		vp = (LazyViewPager) findViewById(R.id.pager);
		vp.setInitTab(initTab);
		
		//1、消息推送  2、短信分享
		if (mCurrentType==2){
			if (isExport) {
				vp.addView(createMobilePhoneView());
				vp.addView(createMyCustomerViewView());
				vp.addView(createPhoneBookViewView());
			}else{
				vp.addView(createMobilePhoneView());
				vp.addView(createPhoneBookViewView());
			}
		}
		if (mCurrentType==1){
			vp.addView(createMyCustomerViewView());
		}
		
		tab = (ViewPagerTab) findViewById(R.id.pagertab);
		tab.setInitTab(initTab);
		
		if (mCurrentType==2){
			if (isExport) {
				tab.addTitle(new String[]{
						paramContext.getResources().getString(names[0]),
						paramContext.getResources().getString(names[1]),
						paramContext.getResources().getString(names[2])});
			}else{
				tab.addTitle(new String[]{
						paramContext.getResources().getString(names[0]),
						paramContext.getResources().getString(names[2])});
			}
		}
		if (mCurrentType==1){
			tab.addTitle(new String[]{paramContext.getResources().getString(names[1])});
		}
		tab.setViewpager(vp);
		vp.setTab(tab);
	}

	public void addView(int paramInt) {
		LayoutInflater.from(getContext()).inflate(paramInt, this);
	}
	
	@Override
	public void clearCache() {
		super.clearCache();
		if (mobilePhoneView!=null)
			mobilePhoneView.clearCache();
		if (myCustomerView!=null)
			myCustomerView.clearCache();
		if (phoneBookView!=null)
			phoneBookView.clearCache();
	}
	
	private MobilePhoneView createMobilePhoneView(){
		
		if ( mobilePhoneView != null ) 
			return mobilePhoneView;
		
		mobilePhoneView = new MobilePhoneView(ctx);	
		mobilePhoneView.setOpenSendListener(mOpenSend);

		return mobilePhoneView;
	}
	
	
	private MyCustomerView createMyCustomerViewView(){
		
		if ( myCustomerView != null ) 
			return myCustomerView;
		
		myCustomerView = new MyCustomerView(ctx, mCurrentType==2);		
		
		myCustomerView.setOpenSendListener(mOpenSend);
		
		return myCustomerView;
	}
	
	
	private PhoneBookView createPhoneBookViewView(){
		
		if ( phoneBookView != null ) 
			return phoneBookView;
		
		phoneBookView = new PhoneBookView(ctx);		
		phoneBookView.setOpenSendListener(mOpenSend);

		return phoneBookView;
	}

	
}


